/**
 * 投放控制列表
 */
define(['../../common/service', './feed', 'text!./eachCatalogError.html'],
    function( http, feed, eachCatalogErrorTmpl){
        var feedOverview = {
            init: function () {
                var _this = this;
                feed._init_tabs(0);
                $('.overview-child-flag-img').css('background-image', 'url(./imgs/overview.png)');
                var weFeedId = window.localStorage.getItem('weFeedId');
                if (weFeedId) {
                    _this.fillOverview(weFeedId);
                }
            }
            /*
             @填充预览界面
             */
            ,fillOverview : function(weFeedId){
                var _this = this;
                var params = {
                    id :weFeedId
                };
                http.work('/feed/overview', params,'POST').then(function(res){
                    if(res.succeed){
                        var feedErrorArr = [];
                        var a1= 0,b1= 0,a2= 0,b2= 0,a3= 0,b3= 0;
                        $.each(res.data, function(i, v){
                            switch (v.level) {
                                case "EVENT_SOURCE":
                                    if(v.result == 'FAILED')
                                        a1 =1;
                                    else if(v.result == 'INTERMEDIATE' || v.result == 'INCOMPLETE')
                                        b1=1;
                                    $(".overview-child").eq(0).append('<div class="content overview-wrapper"><div class="overview-content">'
                                    + '<div class="overview-title">'+ v.title +'</div>'
                                    +'<div class="overview-description">'+ v.description +'</div></div></div>');
                                    break;
                                case "PRODUCT_FEED":
                                    if(v.fbProductCatalogId)
                                        feedErrorArr.push(v);
                                    else
                                        $('#wefeederror').text(v.title+"："+ v.description);

                                    if(v.result == 'fatal')
                                        a2 =1;
                                    else if(v.result == 'warning' || v.result == 'INCOMPLETE')
                                        b2=1;
                                    break;
                                case "PRODUCT_SET":
                                    if(v.result == 'FAILED')
                                        a3 =1;
                                    else if(v.result == 'INTERMEDIATE' || v.result == 'INCOMPLETE')
                                        b3=1;
                                    $(".overview-child").eq(2).append('<div class="content overview-wrapper"><div class="overview-content">'
                                    + '<div class="overview-title">'+ v.title +'</div>'
                                    +'<div class="overview-description">'+ v.description +'</div></div></div>');
                                    break;
                            }
                        });
                        if(a1==0 && b1 ==0)
                            $(".overview-child-flag-img").eq(0).addClass('pass');
                        else if (a1==0 && b1 == 1)
                            $(".overview-child-flag-img").eq(0).addClass('warn');
                        else
                            $(".overview-child-flag-img").eq(0).addClass('error');

                        if(a2==0 && b2 ==0)
                            $(".overview-child-flag-img").eq(1).addClass('pass');
                        else if (a2==0 && b2 == 1)
                            $(".overview-child-flag-img").eq(1).addClass('warn');
                        else
                            $(".overview-child-flag-img").eq(1).addClass('error');

                        if(a3==0 && b3 ==0)
                            $(".overview-child-flag-img").eq(2).addClass('pass');
                        else if (a3==0 && b3 == 1)
                            $(".overview-child-flag-img").eq(2).addClass('warn');
                        else
                            $(".overview-child-flag-img").eq(2).addClass('error');

                        var elementGroup = _.groupBy(feedErrorArr, function(o){return o.productCatalogName});
                        var elementGroupArr = [];
                        //填充未上传产品数
                        var weFeedProductCnt = window.localStorage.getItem('weFeedProductCnt');
                        for(var q in elementGroup) {
                            var cnt = 0;
                            var percent = '';
                            _.each(elementGroup[q], function(v) {
                                if(v.result == 'fatal') {
                                    var totalCnt = (typeof(v.totalCount) === "undefined") ? 0:v.totalCount;
                                    cnt += totalCnt;
                                }
                            });
                            if(cnt != 0 && weFeedProductCnt != 0) {
                                if(cnt>weFeedProductCnt){
                                    percent = '(100%)';
                                } else {
                                    percent = '(' + Math.round(cnt/weFeedProductCnt * 10000) / 100.00 + '%)';
                                }
                            }
                            elementGroupArr.push({
                                name: q,
                                cnt1: (cnt>weFeedProductCnt?weFeedProductCnt:(weFeedProductCnt-cnt)),
                                cnt2: weFeedProductCnt,
                                cnt3: cnt>weFeedProductCnt?weFeedProductCnt:cnt,
                                cnt4: percent,
                                list: elementGroup[q]
                            });
                        }
                        var elementGroupEach = '';
                        _.each(elementGroupArr, function(el) {
                            elementGroupEach += _.template(I18N.parse(eachCatalogErrorTmpl,'feedOverview'))({d: el});
                        });
                        $(".overview-child").eq(1).append(elementGroupEach);

                        $('.overview-click-title-box').off('click').on('click', function(){
                            var imgDiv = $(this).find('img');
                            if(imgDiv.hasClass('click-down')) {
                                imgDiv.removeClass('click-down');
                                $(this).next().addClass('hidden');
                            } else {
                                imgDiv.addClass('click-down');
                                $(this).next().removeClass('hidden');
                            }
                        });

                    }
                });

            }
        }
        return feedOverview;
    });
